package com.turbo.boot.crawler.spider.worker;

import com.turbo.boot.crawler.dao.dal.CrawlerConfigDal;
import com.turbo.boot.crawler.dao.entity.CrawlerConfigEntity;
import com.turbo.boot.crawler.spider.TurboCrawler;
import com.xuxueli.crawler.XxlCrawler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 描述
 *
 * @author wuyn
 * @since 2024/4/17
 */
public abstract class BaseTurboCrawler<PageVo> implements TurboCrawler<PageVo> {


    @Resource
    protected CrawlerConfigDal crawlerConfigDal;


    protected XxlCrawler.Builder xxlCrawlerBuilder = new XxlCrawler.Builder();

    protected XxlCrawler crawler = null;

    @Resource
    protected Map<String, BaseTurboCrawlerPageParser> pageParserMap;


    public boolean build() {
        CrawlerConfigEntity configEntity = crawlerConfigDal.selectByCrawlerRef(name());
        if (null == configEntity) {
            return false;
        }

        xxlCrawlerBuilder.setUrls(configEntity.getTargetUrls())
                .setWhiteUrlRegexs(configEntity.getWhiteUrlRegexs())
                .setThreadCount(configEntity.getThreadCounts())
                .setPageParser(getPageParser());
        return true;
    }


    @Override
    public void crawl() {
        if (null == crawler) {
            boolean buildResult = build();
            if (!buildResult) {
                return;
            }
            crawler = xxlCrawlerBuilder.build();
        }
        crawler.start(true);
    }


    @Override
    public String name() {
        return this.getClass().getAnnotation(Service.class).value();
    }


    /**
     * 获取处理器
     *
     * @return
     */
    protected BaseTurboCrawlerPageParser<PageVo> getPageParser() {
        return pageParserMap.get(name() + "PageParser");
    }

}
